package cn.iocoder.yudao.module.def.controller.admin.attachment;

import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;


import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.def.controller.admin.attachment.vo.*;
import cn.iocoder.yudao.module.def.dal.dataobject.attachment.AttachmentDO;
import cn.iocoder.yudao.module.def.service.attachment.AttachmentService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 附件")
@RestController
@RequestMapping("/def/attachment")
@Validated
public class AttachmentController {

    @Resource
    private AttachmentService attachmentService;

    @PostMapping("/create")
    @Operation(summary = "创建附件")
    @PreAuthorize("@ss.hasPermission('def:attachment:create')")
    public CommonResult<Integer> createAttachment(@Valid @RequestBody AttachmentSaveReqVO createReqVO) {
        return success(attachmentService.createAttachment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新附件")
    @PreAuthorize("@ss.hasPermission('def:attachment:update')")
    public CommonResult<Boolean> updateAttachment(@Valid @RequestBody AttachmentSaveReqVO updateReqVO) {
        attachmentService.updateAttachment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除附件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('def:attachment:delete')")
    public CommonResult<Boolean> deleteAttachment(@RequestParam("id") Integer id) {
        attachmentService.deleteAttachment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得附件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('def:attachment:query')")
    public CommonResult<AttachmentRespVO> getAttachment(@RequestParam("id") Integer id) {
        AttachmentDO attachment = attachmentService.getAttachment(id);
        return success(BeanUtils.toBean(attachment, AttachmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得附件分页")
    @PreAuthorize("@ss.hasPermission('def:attachment:query')")
    public CommonResult<PageResult<AttachmentRespVO>> getAttachmentPage(@Valid AttachmentPageReqVO pageReqVO) {
        PageResult<AttachmentDO> pageResult = attachmentService.getAttachmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AttachmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出附件 Excel")
    @PreAuthorize("@ss.hasPermission('def:attachment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAttachmentExcel(@Valid AttachmentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AttachmentDO> list = attachmentService.getAttachmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "附件.xls", "数据", AttachmentRespVO.class,
                        BeanUtils.toBean(list, AttachmentRespVO.class));
    }

}